clear all
close all
clc

nIter = 30;
filename_pattern = 'jpg/%08d.jpg';

%javaaddpath('zxing\core-2.2.jar');
%javaaddpath('zxing\javase-2.2.jar');

%javaaddpath([pwd, '\qr_code\dec\zxing\core-2.2.jar']);
%javaaddpath([pwd, '\qr_code\dec\zxing\javase-2.2.jar']);


% determine the number of decoded frames
nFrames = PatFileName(filename_pattern);

tic
A = linspace(-90, 90, nIter);
decmsg = char(ones(1024,1024)*255);
decmsg = decmsg(:);
decidx = 0;
nStart = 0;
tmpvar = zeros(3, 4);
DecFail = 0;

for n = 1:nFrames
    filename = sprintf(filename_pattern, n);
    I = rgb2gray(imread(filename));
    
    % decoding images
    for k = 1:nIter
        ang = A(k);
        J = imrotate(I, ang);
        message = decode_qr(J);
        if ~isempty(message)
            break;
        end
    end
    
    if isempty(message)
        fprintf('QR decode failed! image index %08d\n', n);
        continue;
    end
    
    % is decoded message is not empty...
    if message(1) == '#'
        if strcmp(message(2:4), 'end')
            fprintf('end of file reached!\n');
            break;
        elseif strcmp(message(2:6), 'start')
            eval(message(7:end));
            if exist('BFsize', 'var') && exist('DataFrameSize', 'var') ...
                    && exist('nImages', 'var') && exist('LastBlock', 'var')
                nStart = nStart+1;
                if nStart <= 3
                    tmpvar(nStart,:) = [BFsize, DataFrameSize, nImages, LastBlock];
                    fprintf('BFsize = %d\n', BFsize);
                    fprintf('DataFrameSize = %d\n', DataFrameSize);
                    fprintf('nImages = %d\n', nImages);
                    fprintf('LastBlock = %d\n', LastBlock);
                end
            else
                fprintf('start frame decoding error (1)!');
            end
        else
            fprintf('start frame decoding error (2)!');
            continue;
        end
    end
    


    if message(1) == '['
        idx = str2double(message(2:7));
        if ~((message(8) == ']') && (message(9) == '<') ...
                && (message(end) == '>'))
            fprintf('%s...%s\n', message(1:16), message(end));
            continue;
        end
        
        if (idx == decidx+1)
            start_idx = decidx*DataFrameSize+1;
            content = message(10:end-1);
            decmsg(start_idx:start_idx+length(content)-1) = content;
            decidx = idx;
        elseif idx == decidx
            fprintf('duplicated frame %d!\n', idx);
        else
            fprintf('decoding error: possible missing frame!\n');
            fprintf('current frame idx = %d\n', idx);
            fprintf('current image idx = %d\n', n);
            DecFail = 1;
            break;
        end
        
        if idx == nImages+1
            break;
        end
    end
    
end

if DecFail == 0
    file_put_contents('decmsg.txt', decmsg(1:nImages*DataFrameSize+LastBlock));
    base64_decoder('decmsg.txt');
end
toc
